import createjs from "createjs-ts"
import cjs=createjs
//console.log('设备宽高：',window.innerWidth,window.innerHeight)
//console.log('设备像素比：',window.devicePixelRatio)
const rewrite=()=>{
    const p=cjs.Stage.prototype as any
    p._getElementRect = function(e:HTMLElement) {
        // e为canvas时改写_getElementRect
        if(e instanceof HTMLCanvasElement)
        return {
            left: 0,
            right: e.clientWidth,
            top: 0,
            bottom: e.clientHeight
        }
        var bounds;
		try { bounds = e.getBoundingClientRect(); } // this can fail on disconnected DOM elements in IE9
		catch (err) { bounds = {top: e.offsetTop, left: e.offsetLeft, width:e.offsetWidth, height:e.offsetHeight}; }

		var offX = (window.pageXOffset || 0) - (document.body.clientLeft || 0);
		var offY = (window.pageYOffset || 0) - (document.body.clientTop  || 0);

		var styles = getComputedStyle(e,null); // IE <9 compatibility.
		var padL = parseInt(styles.paddingLeft)+parseInt(styles.borderLeftWidth);
		var padT = parseInt(styles.paddingTop)+parseInt(styles.borderTopWidth);
		var padR = parseInt(styles.paddingRight)+parseInt(styles.borderRightWidth);
		var padB = parseInt(styles.paddingBottom)+parseInt(styles.borderBottomWidth);

		// note: in some browsers bounds properties are read only.
		return {
			left: bounds.left+offX+padL,
			right: bounds.right+offX-padR,
			top: bounds.top+offY+padT,
			bottom: bounds.bottom+offY-padB
		}
    };
    p._updatePointerPosition = function(id:number, e:TouchEvent, pageX:number, pageY:number) {
        var rect = this._getElementRect(this.canvas);
        const temp=pageX
        pageX=pageY
        pageY=window.innerWidth-temp

        pageX -= rect.left;
        pageY -= rect.top;
    
        var w = this.canvas.width;
        var h = this.canvas.height;
        pageX /= (rect.right-rect.left)/w;
        pageY /= (rect.bottom-rect.top)/h;
        var o = this._getPointerData(id);
        if (o.inBounds = (pageX >= 0 && pageY >= 0 && pageX <= w-1 && pageY <= h-1)) {
            o.x = pageX;
            o.y = pageY;
        } else if (this.mouseMoveOutside) {
            o.x = pageX < 0 ? 0 : (pageX > w-1 ? w-1 : pageX);
            o.y = pageY < 0 ? 0 : (pageY > h-1 ? h-1 : pageY);
        }
    
        o.posEvtObj = e;
        o.rawX = pageX;
        o.rawY = pageY;
    
        if (id === this._primaryPointerID || id === -1) {
            this.mouseX = o.x;
            this.mouseY = o.y;
            this.mouseInBounds = o.inBounds;
        }
    };
}
export {rewrite}